<?php

    include('getredirects.php');
    include('getCardProperties.php');
    
    try 
    {
        // Let's check whether we can perform the magick. 
        if (TRUE !== extension_loaded('imagick')) 
        { 
            throw new Exception('Imagick extension is not loaded.'); 
            echo "Imagick not accessible";
        } 
        
        /* Alternate way to check for ImageMagick existance
        // This check is an alternative to the previous one. 
        if (TRUE !== class_exists('Imagick')) 
        { 
            throw new Exception('Imagick class does not exist.'); 
            echo "Imagick not accessible";
        } 
        */
        
        // Read card base file
        $cardBaseFile = 'assets/images/cardBases/fb_trump_card_proto1.png';
        $cardBase = new Imagick($cardBaseFile); 
        
        // Find width and height of the card base
        list($cardBaseWidth, $cardBaseHeight, $type, $attr)= getimagesize($cardBaseFile);
        //echo "base width = ".$cardBaseWidth . "height = ".$cardBaseHeight; 
        
        // Indirect link to the large profile picture
        $virtualProfilePictureURL = $fb_graph_url . $user . '/picture?type=large';

        // Get the redirected actual profile image path
        $actualProfilePictureURL = get_all_redirects($virtualProfilePictureURL);
        
        // Get the large profile picture of the user
        $largeSizeProfilePictureData = file_get_contents($actualProfilePictureURL[0]);
        
        // Save the large profile picture with the name "fbid".jpg
        $largeSizeProfilePictureFile = 'assets/images/largeProfilePictures/'.$user.'.jpg';
        file_put_contents($largeSizeProfilePictureFile, $largeSizeProfilePictureData);
        
        $largeSizeProfilePicture = new Imagick($largeSizeProfilePictureFile);  
        
        $largeSizeProfilePicture->scaleImage(210, 200, false);
        
        // Find width and height of the large profile picture
        list($largeProfilePictureWidth, $largeProfilePictureHeight, $type, $attr)= getimagesize($largeSizeProfilePictureFile);
        //echo "profile width = ".$largeProfilePictureWidth . "height = ".$largeProfilePictureHeight; 
        
        // Let's compose the final trump card          
             
        // Position the profile picture     
        $pixelsFromLeft = 50;   // <50><210><50> = 310
        $pixelsFromTop = 20;    // <20><200><290> = 510
        $cardBase->compositeImage($largeSizeProfilePicture, Imagick::COMPOSITE_DEFAULT, $pixelsFromLeft, $pixelsFromTop); 

        // Let's merge all layers (it is not mandatory). 
        $cardBase->flattenImages(); 

        // Save the composite cardbase + large profile image
        $finalTrumpCardFile = 'assets/images/trumpCards/'.$user."_trumpCard.png";
        $cardBase->setImageFileName($finalTrumpCardFile); 
        
        // Let's write the image. 
        if  (FALSE == $cardBase->writeImage()) 
        {
            throw new Exception(); 
        } 
        
        // Add text
        // a new imagick object
        $im = new Imagick($finalTrumpCardFile);

        // Create a drawing object and set the font size
        $draw = new ImagickDraw();
        
        // PLAYER NAME : set the font, fontsize, color, position, translation
        $draw->setFont( "assets/fonts/Generator REX.ttf" );
        $draw->setFontSize( 30 );
        $draw->setFillColor('white');
        $draw->setGravity( Imagick::GRAVITY_CENTER );
        $draw->translate(0.0, 0.0);
        // overlay the player name text on the image
        $im->annotateImage( $draw, 0, -30, 0, strtoupper($user_profile[first_name]) );
        
        // Set the font, fontsize, color, position, translation
        
        // FRIENDS : overlay the 'Friends' text on the image
        $draw->setFont( "assets/fonts/AllAgesDEMO.ttf" );
        $draw->setFontSize( 24 );
        $draw->setFillColor('white');
        $draw->setGravity( 4 );
        $draw->translate(0.0, 0.0);
        $im->annotateImage( $draw, 18, 12, 0, 'Friends' );
        
        // LIKES : overlay the 'Likes' text on the image
        $im->annotateImage( $draw, 18, 74, 0, 'Likes' );
       
        // ALBUMS : overlay the 'Albums' text on the image
        $im->annotateImage( $draw, 18, 142, 0, 'Albums' );
        
        // GROUPS : overlay the 'Groups' text on the image
        $im->annotateImage( $draw, 18, 213, 0, 'Groups' );
        
        
        // FRIENDS COUNT : overlay the 'Friends' text on the image
        $draw->setFont( "assets/fonts/All Star Resort.ttf" );
        $draw->setFontSize( 28 );
        $draw->setGravity( 4 );
        $draw->translate(0.0, 0.0);
        $im->annotateImage( $draw, 230, 12, 0, $friends_count );
        
        // LIKES COUNT : overlay the 'Likes' text on the image
        $im->annotateImage( $draw, 230, 74, 0, $likes_count );
       
        // ALBUMS COUNT : overlay the 'Albums' text on the image
        $im->annotateImage( $draw, 230, 142, 0, $albums_count );
        
        // GROUPS COUNT : overlay the 'Groups' text on the image
        $im->annotateImage( $draw, 230, 213, 0, $groups_count );
        
        
        // set image format to png
        $im->setImageFormat( "png" );

        $im->scaleImage(260, 380, false);
        
        // write image to disk
        $im->writeImage( 'assets/images/trumpCards/'.$user.'_trumpCard.png' );
        
    } 

    catch (Exception $e) 
    { 
        echo 'Caught exception: ' . $e->getMessage() . "\n"; 
    }  
   
    // Add "Game title" text to game back
    
    try
    {
        // a new imagick object
        $gameBackFile = "assets/images/gameBacks/gameBack.png";
        $im = new Imagick($gameBackFile);

        // Create a drawing object and set the font size
        $draw = new ImagickDraw();
        
        /*
        // GAME TITLE NAME : set the font, fontsize, color, position, translation
        $draw->setFont( "assets/fonts/Blox2.ttf" );
        $draw->setFontSize( 70 );
        $draw->setGravity( 3 );
        $draw->translate(0.0, 0.0);
        $im->annotateImage( $draw, 265, 0, 0, "TRUMPSTERS" );
        
        $draw->setFont( "assets/fonts/@El&Font Destroy!.ttf" );
        $draw->setFontSize( 50 );
        $draw->setFillColor('FireBrick');
        $draw->setGravity( 5 );
        $draw->translate(0.0, 0.0);
        // overlay the player name text on the image
        $im->annotateImage( $draw, 0, 0, 45, "LETS FIGHT" );
        */
        
        $draw->setFont( "assets/fonts/Ascent 2 Stardom.ttf" );
        $draw->setFontSize( 45 );
        $draw->setFillColor('Teal');
        $draw->setGravity( 3 );
        $draw->translate(0.0, 0.0);
        // overlay the player name text on the image
        $im->annotateImage( $draw, 10, 350, 90, strtoupper($user_profile[last_name]) );
        
        
        // set image format to png
        $im->setImageFormat( "png" );

        $im->scaleImage(748, 467, false);
        
        // write image to disk
        $im->writeImage( 'assets/images/gameBacks/'.$user.'_gameBack.png' );
        
        // Wall
        // a new imagick object
        $wallFile = "assets/images/buttons/wallPlain.jpg";
        $im = new Imagick($wallFile);

        // Create a drawing object and set the font size
        $draw = new ImagickDraw();
        $im->scaleImage(600, 75, false);
        
        $im->setImageFormat( "jpg" );
        
        $im->roundCorners(15,10);
        
        // Click wall to Post to WALL : overlay the 'Friends' text on the image
        $draw->setFont( "assets/fonts/AllAgesDEMO.ttf" );
        $draw->setFontSize( 24 );
        $draw->setFillColor('white');
        $draw->setGravity( Imagick::GRAVITY_CENTER );
        $draw->translate(0.0, 0.0);
        $im->annotateImage( $draw, 0, 0, 0, 'Click   wall   to   Post   to   WALL' );
        
        // write image to disk
        $im->writeImage( 'assets/images/buttons/wall.jpg' );
        
    }
    catch (Exception $e) 
    { 
        echo 'Caught exception: ' . $e->getMessage() . "\n"; 
    }  
    
?>

